const path = require('path');
const fs = require('fs');

const worksacePath = './packages';
const getAlias = () => {
  const aliasObj = {};
  const packages = fs.readdirSync(worksacePath);
  packages.forEach((item) => {
    const packagePath = path.join(worksacePath, item, 'package.json');
    const aliasPath = path.join(worksacePath, item, 'src');
    if (fs.existsSync(`./${packagePath}`)) {
      const pkg = JSON.parse(fs.readFileSync(`./${packagePath}`, 'utf-8'));
      aliasObj[pkg.name] = path.join(__dirname, aliasPath);
    } else {
      console.log(`${packagePath} 路径不存在`);
      throw new Error(`${packagePath} 路径不存在`);
    }
  });
  return aliasObj;
};

module.exports = getAlias;
